package ro.gateway.aida.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Organization;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.srv.EditorServlet;
import ro.gateway.aida.srv.ICFService;
import ro.gateway.aida.utils.HttpUtils;
import ro.gateway.aida.utils.LocalisedSortableList;

/**
 * @deprecated
 * @author Mihai Postelnicu
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class OrgSelectorServlet
	  extends EditorServlet
{
	  public OrgSelectorServlet()
	  {
	  }

	  /**
	   * This should generate the common objects for displaying the page,
	   * such as the dropdown and the (empty) selected org list.
	   * This is required because edit () action cannot take advantage of the
	   * initialization in start_editing()
	   */

	  protected void generateCommon( HttpServletRequest request,
									 ICFService cfService )
	  {
			HttpSession session = request.getSession();
			ServletContext application = this.getServletContext();
			Hashtable beans = ( Hashtable ) session.getAttribute( PNAME_BEANS );
			Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
			String action = ( String ) bean.get( PNAME_ACTION );
			String key = ( String ) request.getAttribute( PNAME_KEY );
			String param = HttpUtils.getValidTrimedString( request, PNAME_PARAM, null );
			long orgstopic = HttpUtils.getLong( request, "orgstopic", -1 );
			String orgskey = HttpUtils.getValidTrimedString( request, "orgskey",
				  "anonymous" );
			PersistenceToken token = PersistenceManager.tokenLookup( application );
			String lang = HttpUtils.getValidTrimedString( request, "lang", "ro" );
			OrganizationDB orgdb = OrganizationDB.getManager(token);
			//if null, generate the dropdown:
			Hashtable orgsels = ( Hashtable ) beans.get( PNAME_ORGSELS );
			if ( orgsels == null )
			{
				  orgsels = new Hashtable();
				  beans.put( PNAME_ORGSELS, orgsels );
			}

//opt
			bean.put( PNAME_ORGSKEY, orgskey );
			Vector orgsel = ( Vector ) bean.get( PNAME_SELECTEDORG );
			if ( orgsel == null )
			{
				  //try to revive orgsel from session info
				  orgsel = ( Vector ) orgsels.get( orgskey );
				  if ( orgsel == null )
				  {
						orgsel = new Vector();
				  }
				  bean.put( PNAME_SELECTEDORG, orgsel );
			}

			LocalisedSortableList list = ( LocalisedSortableList ) bean.get( PNAME_ORGCATLIST );
			try
			{
				  if ( list == null )
				  {
						list = orgdb.getList();
						list.setComparator(new Organization.LangComparator(lang));
						list.setViewLang(lang);
						
						bean.put( PNAME_VERBOSE, "all" );
				  }
				  if ( orgstopic != 0 && orgstopic != -1 && !( "add".equals( param ) ) )
				  {
						list = orgdb.getByCat( orgstopic ,lang);
						bean.put( PNAME_VERBOSE, "byCat" );
				  }
			}
			catch ( SQLException ex )
			{
				  ex.printStackTrace();
			}
			bean.put( PNAME_ORGCATLIST, list );
	  }


	  protected void edit( HttpServletRequest request, HttpServletResponse response ) throws
			javax.servlet.ServletException, java.io.IOException
	  {
			//is this a search refresh ?
			PersistenceToken token = PersistenceManager.tokenLookup( getServletContext() );
			String search = HttpUtils.getValidTrimedString( request, "search", null );
			Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
			String lang = HttpUtils.getValidTrimedString( request, "lang", "ro" );
			String topicch = HttpUtils.getValidTrimedString( request, "topicch", null );
			long orgstopic = HttpUtils.getLong( request, "orgstopic", -1 );
			//   String id= HttpUtils.getValidTrimedString(request,"id",null);
			ServletContext application = this.getServletContext();

			ICFService cfService = ( ICFService )
				  ( ( Hashtable ) application.getAttribute( Constants.SERVICES ) ).get(
						Constants.SERVICE_CF );
			OrganizationDB orgdb = OrganizationDB.getManager(token);

			generateCommon( request, cfService );

			LocalisedSortableList list = ( LocalisedSortableList ) bean.get( PNAME_ORGCATLIST );

			if ( topicch != null )
			{
				  try
				  {
						if ( orgstopic != -1 )
						{
							  list = orgdb.getByCat( orgstopic,lang);
							  
							  bean.put( PNAME_VERBOSE, "byCat" );
						}
						else
						{
							  list = orgdb.getList();
							  list.setComparator(new Organization.LangComparator(lang));
							  list.setViewLang(lang);
							  bean.put( PNAME_VERBOSE, "all" );
						}
				  }
				  catch ( SQLException ex )
				  {
						ex.printStackTrace();
				  }
				  bean.put( PNAME_ORGCATLIST, list );
				  request.setAttribute( PNAME_SCREEN, PV_SCR_EDIT );
				  request.getRequestDispatcher( JSP_PAGE ).
						forward( request, response );
				  return;
			}

			if ( search != null )
			{
				  //yes.it is ! refresh the selector
				  /*try
				  {
						list = orgdb.getByLike( search );
						bean.put( PNAME_ORGCATLIST, list );
				  }
				  catch ( SQLException ex )
				  {
						ex.printStackTrace();
				  }
				  */
			}
			else
			{
				  //no,it's just an add or edit continue
				  long orgselect = HttpUtils.getLong( request, "orgselect", -1 );

				  //read the selected orgs
				  if ( orgselect == -1 )
				  {
						//edit request - i have the beans in memory but i return to edit it.
				  }
				  else
				  {
						//i've seleted an item and now add it to the list
					
				  }

			}

			request.setAttribute( PNAME_SCREEN, PV_SCR_EDIT );
			request.getRequestDispatcher( JSP_PAGE ).
				  forward( request, response );
	  }

	  protected void other_action_performed( HttpServletRequest request,
											 HttpServletResponse response,
											 String action ) throws javax.servlet.
			ServletException, java.io.IOException
	  {
	  }

	  protected void confirm( HttpServletRequest request,
							  HttpServletResponse response ) throws javax.servlet.
			ServletException, java.io.IOException
	  {
			//confirm here resets everything :
			ServletContext application = this.getServletContext();
			PersistenceToken token = PersistenceManager.tokenLookup( application );
			Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
			ICFService cfService = ( ICFService )
				  ( ( Hashtable ) application.getAttribute( Constants.SERVICES ) ).get(
						Constants.SERVICE_CF );
			long orgstopic = HttpUtils.getLong( request, "orgstopic", -1 );
			String lang = HttpUtils.getValidTrimedString( request, "lang", "ro" );
			OrganizationDB orgdb = OrganizationDB.getManager(token);
			String param = HttpUtils.getValidTrimedString( request, PNAME_PARAM, null );
			LocalisedSortableList list = null;

			try
			{

				  if ( "showall".equals( param ) || orgstopic == -1 )
				  {
						list = orgdb.getList();
						bean.put( PNAME_VERBOSE, "all" );
				  }
				  else if ( orgstopic != -1 )
				  {
						list = orgdb.getByCat( orgstopic,lang );
						bean.put( PNAME_VERBOSE, "byCat" );
				  }


			}
			catch ( SQLException ex )
			{
				  ex.printStackTrace();
			}


			if ( "clear".equals( param ) )
			{
				  Vector selected = new Vector();
				  bean.put( PNAME_SELECTEDORG, selected );
			}

			if ( list != null ) bean.put( PNAME_ORGCATLIST, list );

			request.setAttribute( PNAME_SCREEN, PV_SCR_EDIT );
			request.getRequestDispatcher( JSP_PAGE ).
				  forward( request, response );
	  }

	  protected void start_editing(
			ServletContext application,
			HttpServletRequest request,
			HttpServletResponse response ) throws IOException, ServletException
	  {
			super.start_editing( application, request, response );

			//get org list
			ICFService cfService = ( ICFService )
				  ( ( Hashtable ) application.getAttribute( Constants.SERVICES ) ).get(
						Constants.SERVICE_CF );

			//generates common objects

			generateCommon( request, cfService );

	  }

	  public String getPNAME_BEANS()
	  {
			return PNAME_BEANS;
	  }

	  protected void cancel(
			HttpServletRequest request,
			HttpServletResponse response ) throws IOException, ServletException
	  {
			super.cancel( request, response );
			//refreshes the selector

	  }

	  protected void save( ServletContext application, HttpServletRequest request,
						   HttpServletResponse response ) throws javax.servlet.
			ServletException, java.io.IOException
	  {
			Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
			Hashtable beans = ( Hashtable ) request.getSession().getAttribute( PNAME_BEANS );
			Hashtable orgsels = ( Hashtable ) beans.get( PNAME_ORGSELS );

			Vector selected = ( Vector ) bean.get( PNAME_SELECTEDORG );

			String action = ( String ) bean.get( PNAME_ACTION );
			String orgskey = HttpUtils.getValidTrimedString( request, "orgskey", null );

			if ( beans == null || selected == null || orgskey == null || orgsels == null )
			{
				  request.setAttribute( PNAME_SCREEN, PV_SCR_ERROR );
				  request.setAttribute( PNAME_ERRORS, "Not a valid ORG Selector Save" );
				  request.getRequestDispatcher( JSP_PAGE ).forward( request, response );
			}

			//put the selected items into the session
			orgsels.put( orgskey, selected );

			request.setAttribute( PNAME_SCREEN, PV_SCR_READY );
			request.getRequestDispatcher( getJSP_PAGE() ).forward( request, response );
			return;

	  }

	  public String getJSP_PAGE()
	  {
			return JSP_PAGE;
	  }

	  public static final String PV_SCR_READY = "redirect to origin of request";
	  public static final String PNAME_PARAM = "param";
	  public static final String PNAME_ORGSKEY = "org select key";
	  public static final String PNAME_BEANS = "SELECTING_ORGANIZATIONS";
	  public static final String PNAME_SELECTEDORG = "ORG Selector List";
	  public static final String PNAME_ORGSELS = "ORG Selected Hastables";
	  public static final String PNAME_ORGCATLIST = "ORG selector list by orgType";
	  public static final String JSP_PAGE = "orgsel.jsp";
	  public static final String PNAME_ERRORS = "errors";
	  public static final String PNAME_VERBOSE = "explanatory text";
}
